package com.fxkj.cloud.drms.ui.controller;

import com.fxkj.cloud.drms.application.CommandSettingHistoryApplicationService;
import com.fxkj.cloud.drms.application.model.vo.CommandSettingHistoryModelView;
import com.fxkj.cloud.drms.application.model.vo.LatestCommandSettingHistoryKvModelView;
import com.fxkj.common.controller.BaseController;
import com.fxkj.common.exception.FxException;
import com.fxkj.common.model.PageServerResponse;
import com.fxkj.common.model.ServerResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * <p>description: </p>
 *
 * @author ww
 * @date 2025-03-25 11:07:06
 */
@RestController
@RequestMapping("/api/v1/commandSettingHistory")
@Tag(name = "命令下发历史表")
public class CommandSettingHistoryController extends BaseController {

    private final CommandSettingHistoryApplicationService commandSettingHistoryApplicationService;

    public CommandSettingHistoryController(CommandSettingHistoryApplicationService commandSettingHistoryApplicationService) {
        this.commandSettingHistoryApplicationService = commandSettingHistoryApplicationService;
    }

    @Operation(summary = "查询", description = "查询", parameters = {
            @Parameter(name = "page", description = "页码", required = false),
            @Parameter(name = "size", description = "页大小", required = false),
            @Parameter(name = "deviceId", description = "设备id", required = false),
            @Parameter(name = "startTime", description = "开始时间", required = false),
            @Parameter(name = "endTime", description = "结束时间", required = false),
            @Parameter(name = "deviceName", description = "设备名称", required = false),
            @Parameter(name = "username", description = "下发人名称", required = false),
    })
    @GetMapping("list")
    public PageServerResponse<CommandSettingHistoryModelView> list(@RequestParam(name = "page", defaultValue = "0") Integer page,
                                                                   @RequestParam(name = "size", defaultValue = "10") Integer size,
                                                                   @RequestParam(name = "deviceId", required = false) UUID deviceId,
                                                                   @RequestParam(name = "attributeType", required = false, defaultValue = "1") Integer attributeType,
                                                                   @RequestParam(name = "startTime", required = true) Long startTime,
                                                                   @RequestParam(name = "endTime", required = true) Long endTime,
                                                                   @RequestParam(name = "deviceName", required = false) String deviceName,
                                                                   @RequestParam(name = "username", required = false) String username) throws FxException {
        UUID orgId = getOrgId();
        return PageServerResponse.success(commandSettingHistoryApplicationService.page(page, size, deviceId, attributeType, startTime, endTime, deviceName, username, orgId));
    }

    @Operation(summary = "获取设备传入值最新的命令设置", description = "获取设备传入值最新的命令设置", parameters = {
            @Parameter(name = "deviceId", description = "设备id", required = true),
            @Parameter(name = "attributeKeys", description = "属性keys", required = true)
    })
    @GetMapping("latestCommandSetting")
    public ServerResponse<List<LatestCommandSettingHistoryKvModelView>> latestCommandSetting(@RequestParam(value = "deviceId", required = true) UUID deviceId,
                                                                                             @RequestParam(value = "attributeKeys", required = false) List<String> attributeKeys) throws FxException {

        if (null == attributeKeys || attributeKeys.isEmpty()) {
            attributeKeys = new ArrayList<>();
        }
        return ServerResponse.success(commandSettingHistoryApplicationService.getLatestCommandSettingHistoryKv(deviceId, attributeKeys, getOrgId()));
    }
}
